package com.sk.m2calc;

import android.app.*;
import android.os.*;
import org.apache.http.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
import android.view.inputmethod.*;
import android.content.*;

public class MainActivity extends Activity 
{
	private double calcMain(String height,String weight)
	{
		return calcMain(Double.valueOf(height),Double.valueOf(weight));
	}
	private double calcMain(double height,double weight)
	{
		return Math.pow(weight,0.425)*Math.pow(height,0.725)*71.84/10000;
	}
	private double resultBuffer = 0.0;
	private double calcMg(double mg)
	{
		return resultBuffer*mg;
	}
	protected void hideInputKeyboard(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}
	private void setup()
	{
		final EditText txH = findViewById(R.id.txHeight);
		final EditText txW = findViewById(R.id.txWeight);
		final EditText txMg = findViewById(R.id.txMg);
		final TextView tvResult = findViewById(R.id.resultTx);
		final Button btnCalcHW = findViewById(R.id.btnCalc);
		final Button btnCalcMg = findViewById(R.id.btnCalc2);
		final Button btnClear = findViewById(R.id.btnClear);
		btnCalcHW.setOnClickListener(new
		OnClickListener()
		{
			public void onClick(View p)
			{
				if(txH.getText().toString().equals("")||
				txW.getText().toString().equals(""))return;
				try{
				double calcR = calcMain(txH.getText().toString(),txW.getText().toString());
				String rst = String.valueOf(calcR);
				rst = rst+ "m²";
				tvResult.setText(rst);
				resultBuffer = calcR;
					Toast.makeText(MainActivity.this,rst,
								   Toast.LENGTH_LONG).show();
					hideInputKeyboard(p);
				}catch(Throwable e)
				{
					Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG)
					.show();
				}
				
			}
		});
		btnCalcMg.setOnClickListener(new
			OnClickListener()
			{
				public void onClick(View p)
				{
					if(txMg.getText().toString().equals(""))return;
					try{
					double theJiLiang = Double.valueOf(txMg.getText().toString());
					double rs = theJiLiang*resultBuffer;
					tvResult.setText(String.valueOf(rs)+"mg");
						Toast.makeText(MainActivity.this,String.valueOf(rs)+"mg",
						Toast.LENGTH_LONG).show();
						hideInputKeyboard(p);
					}
					catch(Throwable e)
					{
						Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG)
							.show();
					}
				}
			});
		btnClear.setOnClickListener(new
			OnClickListener()
			{
				public void onClick(View p)
				{
					txH.setText("");
					txW.setText("");
					txMg.setText("");
				}
			});
	}
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		setup();
    }
}
